package com.sys.dto;

import com.sys.dto.idto.IProductDTO;
import com.sys.entity.OrdersEntity;
import com.sys.entity.ProductEntity;
import com.sys.entity.UserEntity;
import jakarta.persistence.*;

import java.util.List;

public class ProductDTO implements IProductDTO {
    @Override
    public void save(ProductEntity entity) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(entity);
        em.getTransaction().commit();
    }

    @Override
    public void delete(Integer id) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        ProductEntity productEntity;

        EntityTransaction transaction = em.getTransaction();
        transaction.begin();
        try {
            productEntity = em.find(ProductEntity.class, id);
            em.remove(productEntity);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
            e.printStackTrace();
        } finally {
            em.close();
        }
    }

    @Override
    public ProductEntity update(ProductEntity entity, Integer pid) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();

        ProductEntity oldentity = em.find(ProductEntity.class, pid);
        EntityTransaction transaction = em.getTransaction();
        if (entity != null) {
            transaction.begin();
            try {
                System.out.println("即将修改的用户: " + oldentity.getProduceName());
                oldentity.setProduceName(entity.getProduceName());
                em.merge(oldentity);
                em.getTransaction().commit();
            } catch (Exception e) {
                em.getTransaction().rollback();
                e.printStackTrace();
            } finally {
                em.close();
                return oldentity;
            }
        }
        return entity;
    }

    @Override
    public ProductEntity findById(Integer id) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        ProductEntity productEntity;
        productEntity = em.find(ProductEntity.class, id);
        if (productEntity != null) {
            System.out.println("查询到的用户: " + productEntity.getProduceName());
        } else {
            System.out.println("未找到用户");
        }
        return productEntity;
    }

    @Override
    public ProductEntity changePassword(Integer id, String password) {
        return null;
    }

    @Override
    public List<ProductEntity> findByProperty(String propertyName, Object value) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        // 使用查询语句来查找用户
        TypedQuery<ProductEntity> query = em.createQuery(
                "SELECT u FROM ProductEntity u WHERE u."+ propertyName + "= : " + propertyName,
                ProductEntity.class
        );
        query.setParameter(propertyName, value);
        List<ProductEntity> productEntities;
        try{
            productEntities = query.getResultList();
            return productEntities;
        }catch (NoResultException e){
            System.out.println("没有找到用户");
            return null;
        }finally {
            em.close();
        }
    }

    @Override
    public ProductEntity findByName(String name) {
        return null;
    }

    @Override
    public List<ProductEntity> findByAge(Object age) {
        return null;
    }

    @Override
    public List<ProductEntity> findAll() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();

// 使用查询语句来查找所有用户
        TypedQuery<ProductEntity> query = em.createQuery(
                "SELECT u FROM ProductEntity u",
                ProductEntity.class
        );

        List<ProductEntity> productEntities = query.getResultList();

        for (ProductEntity productEntity : productEntities) {
            System.out.println("找到的用户: " + productEntity.getProduceName());
        }

        return productEntities;
    }
}

